﻿using System.Globalization;
using System.Xml;
using System.Xml.Linq;
using WMM.Tapioca.Taxonomy;

namespace WMM.Tapioca.Linkbase
{
	/// <summary>
    /// XBRL 2.1 Specification 5.2.2.2
	/// An encapsulation of the XBRL element "label" as defined in the http://www.xbrl.org/2003/linkbase namespace. 
	/// </summary>
    public class Label : Resource
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

		/// <summary>
		/// The text of this label.
		/// </summary>
		public string Text
		{
			get;
			private set;
		}

		/// <summary>
		/// The culture of this label.
		/// </summary>
		public CultureInfo Culture
		{
			get;
			private set;
		}


        internal Label(XElement labelElement, string label, string linkbaseSource, ExtendedLink extendedLink, DTS  dts)
            : base(labelElement, label, linkbaseSource, extendedLink)
        {
            this.Id = label;
            this.Text = labelElement.Value;

            XNamespace nsXLink = labelElement.GetNamespaceOfPrefix("xlink");
            this.Role = (string)labelElement.Attribute(nsXLink + "role") ?? string.Empty;

            XNamespace nsXml = labelElement.GetNamespaceOfPrefix("xml");
            string languageValue = (string)labelElement.Attribute(nsXml + "lang") ?? string.Empty;

            try
            {
                this.Culture = new CultureInfo(languageValue);
            }
            catch (CultureNotFoundException ex)
            {
                log.Error("Validation error on create linkbase", ex);
                dts.ValidatedLinkbases = false;
            }
        }

    }
}
